#include <stdio.h>
#include "kfifo.h"
#include "main.h"	
#include "msg.h"
#include "key.h"
#include "sw_timer.h"


//KEY	PC13
//KEY2	PC14


uint8_t key_id = 0;
uint16_t key_count = 0;
uint8_t key_multihit_index = 0;

uint8_t key_timer = 0xff;
uint8_t key_multihit_timer = 0xff;



void app_key_gpio_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOC_CLK_ENABLE();
  
    /*Configure GPIO pin : PC13 PC14*/
    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    
    HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}

void app_key_gpio_deinit(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOC_CLK_ENABLE();
  
    /*Configure GPIO pin : PC13 PC14*/
    GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    //HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
    HAL_NVIC_DisableIRQ(EXTI15_10_IRQn);
}


uint8_t app_key_get_id(void)
{
    if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == 0)
        return KEY_ID_PRIMARY;
    else if(HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_14) == 0)
        return KEY_ID_SECONDARY;
    else
        return 0xff;
}

void app_key_gpio_irq(void)
{	
	uint8_t status = app_key_get_id();
	//BOX_LOG("key irq: %d, %d\n", status, key_timer);
	
	if((status != 0xff) && (key_timer == 0xff))
        key_timer = sw_timer_start(KEY_SHAKE_TIMES, 1, app_key_timer_callback);
}

void app_key_timer_stop(void)
{
    if(key_timer != 0xff)
        key_timer = sw_timer_stop(key_timer);
}

void app_key_timer_callback(void)
{
    uint8_t cur_key_id = app_key_get_id();
    
	if(cur_key_id != 0xff)
	{
		if(key_id == 0xff)
		{
            key_id = cur_key_id;
			key_count = 0;
            
            if(key_timer != 0xff)
                sw_timer_update(key_timer, KEY_SCAN_TIMES);
		}
        else if(key_id != cur_key_id)
        {
            if(key_multihit_timer != 0xff)
            {
                key_multihit_timer = sw_timer_stop(key_multihit_timer);
                app_key_multihit_timer_callback();
            }
            key_id = cur_key_id;
            key_count = 0;
            if(key_timer != 0xff)
                sw_timer_update(key_timer, KEY_SCAN_TIMES);
        }
		else 
		{
			key_count++;
			if(key_count == KEY_LONG_1S_COUNT)
				app_key_send_event(key_id, MSG_KEY_LONG_1S);
			else if(key_count == KEY_LONG_3S_COUNT)
				app_key_send_event(key_id, MSG_KEY_LONG_3S);
			else if(key_count == KEY_LONG_6S_COUNT)
				app_key_send_event(key_id, MSG_KEY_LONG_6S);
			else if(key_count == KEY_LONG_10S_COUNT)
				app_key_send_event(key_id, MSG_KEY_LONG_10S);
		}
	}
	else
	{
		if(key_count > 0 && key_count < 20)
		{
			key_multihit_index++;
            if(key_multihit_timer != 0xff)
                key_multihit_timer = sw_timer_stop(key_multihit_timer);
			key_multihit_timer = sw_timer_start(KEY_MULTIHIT_TIMES, 0, app_key_multihit_timer_callback);
		}
        else
        {
            key_id = 0xff;
        }
        
		key_count = 0;
		app_key_timer_stop();
		//BOX_LOG("key up\n");
	}
}

void app_key_multihit_timer_callback(void)
{
    key_multihit_timer = 0xff;
    
	if(key_multihit_index > 0 && key_multihit_index <= 3)
		app_key_send_event(key_id, MSG_KEY_CLICK + (key_multihit_index-1));
	else
		BOX_LOG("key mult err: %d\n", key_multihit_index);

    key_id = 0xff;
	key_multihit_index = 0;
}

void app_key_send_event(uint8_t id, uint8_t event)
{
    if(id == KEY_ID_PRIMARY)
        app_msg_send(event);
    else
        BOX_LOG("secondary key: %d\n", event);
}

void app_key_init(void)
{
    app_key_gpio_init();
}

void app_key_deinit(void)
{
    app_key_gpio_deinit();
}


